{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b1263b7a-0828-43ff-a445-a40a4966fbc3",
   "metadata": {},
   "source": [
    "# 一、Python基础\n",
    "## 1.1、Python的输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6255b109-8b99-4a61-82cf-742fa404ca7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "print(\"hello world!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60c04367-29a2-48a7-ae57-2342ec08a33e",
   "metadata": {},
   "source": [
    "Python声明变量不需要定义数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "37c3c9bc-205a-4abb-9212-bc7924c3f437",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello 12.33 10\n"
     ]
    }
   ],
   "source": [
    "a = 10 #\n",
    "b = \"hello\"\n",
    "a = \"10\"\n",
    "c = 12.33\n",
    "print(b,c,a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "96b4c7ad-9ca0-4a0a-a9ee-e2c66b1222b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2349470870096\n",
      "10\n",
      "<class 'int'>\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "print(id(a)) #可以看a的id\n",
    "print(a)\n",
    "print(type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "16ab342e-6af7-4221-9454-bd82a0bde00f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2349470870096\n"
     ]
    }
   ],
   "source": [
    "b = 10\n",
    "print(id(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "67498ae4-3908-4f3f-b262-f51fb38f2d93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2349470870416 2349470870096\n"
     ]
    }
   ],
   "source": [
    "b = 20\n",
    "print(id(b),id(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7803c348-0bcc-42d8-a492-8ab21a1741a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'> 2349555217392\n",
      "<class 'str'> 2349555218064\n",
      "<class 'str'> 2349555217200\n"
     ]
    }
   ],
   "source": [
    "s1 = \"张三\"\n",
    "print(type(s1),id(s1)) #类型是str\n",
    "s1 = \"李四\"\n",
    "print(type(s1),id(s1))\n",
    "s1 = \"张三\"\n",
    "print(type(s1),id(s1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "dc77082d-a494-4f9f-944b-429c9d9749e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2349555108240\n",
      "2349470870736\n",
      "2349555109040\n"
     ]
    }
   ],
   "source": [
    "a = 2000\n",
    "print(id(a))\n",
    "a = 30\n",
    "print(id(a))\n",
    "a = 2000\n",
    "print(id(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dec9d2bd-7431-436c-998d-c3eafceb2580",
   "metadata": {},
   "source": [
    "## 1.2、Python的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0404207d-5935-43e1-9704-6e695329abe6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "140711869540456 140711869540488 140711869540456\n"
     ]
    }
   ],
   "source": [
    "# 定义一个bool类型\n",
    "a = True\n",
    "b = False\n",
    "c = True\n",
    "print(id(a),id(b),id(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "064d12d9-7618-4174-8f6f-83d0c30aec42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.1 2.2 3.3\n"
     ]
    }
   ],
   "source": [
    "a1 = 1.1\n",
    "b1 = 2.2\n",
    "from decimal import Decimal\n",
    "print(a1,b1,Decimal('1.1')+Decimal('2.2')) #Decimal会把字符串转换为小数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e536cf8e-1ff8-40bc-8463-fc00ef3ab84f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1020\n",
      "30\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = \"20\"\n",
    "# print(a+b) #在python中不同的数据类型不能相加\n",
    "print(str(a)+b) #把a转换为字符串\n",
    "print(a+int(b)) #把b转换为int"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f027a160-9a9b-434f-92f3-f4d568fd4747",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名是:张三,年龄是:22\n",
      "姓名是张三，年龄是22\n",
      "姓名是张三,年龄是22\n"
     ]
    }
   ],
   "source": [
    "name = \"张三\"\n",
    "age = 22\n",
    "print(\"姓名是:\"+name+\",年龄是:\"+str(age)) #需要强制转换为字符串才能连加\n",
    "#使用c语言的通配符方法\n",
    "print(\"姓名是%s，年龄是%d\"%(name,age))\n",
    "print(\"姓名是{},年龄是{}\".format(name,age))\n",
    "print(f\"姓名是{name},年龄是{age}\") #用得最多的f通配符，3.6之后才能使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "33222c3c-7b99-4544-b12c-b18d7e8f503f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入姓名: 张三\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入年龄: 22\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22 <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "name = input(\"请输入姓名:\")\n",
    "print(name)\n",
    "age = int(input(\"请输入年龄:\"))\n",
    "print(age,type(age)) #input的所有类型都是字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f85242d-707c-44e9-8aee-0ff86a74e6ab",
   "metadata": {},
   "source": [
    "练习:输入姓名和年龄，用格式化字符串打印：\n",
    "“你好，张三，你今年20岁”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "d45a6a0e-4ebf-49ce-9f7c-4971e652d92c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入姓名: 李四\n",
      "输入年龄: 23\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好,李四,你今年23岁\n"
     ]
    }
   ],
   "source": [
    "name = input(\"输入姓名:\")\n",
    "age = int(input(\"输入年龄:\"))\n",
    "print(f\"你好,{name},你今年{age}岁\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "688e6136-04fd-4b0b-8910-be90338bf4cb",
   "metadata": {},
   "source": [
    "## 1.3、运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1d97987-d62b-4217-a3cf-a580625baa92",
   "metadata": {},
   "source": [
    "### 1.3.1、基础运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "4a4079e3-b670-4f3a-b85e-3895e38d5574",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5\n",
      "3\n",
      "1\n",
      "12\n",
      "8\n",
      "$$$$$$$$$$\n"
     ]
    }
   ],
   "source": [
    "print(1/2) #自动转换为float\n",
    "print(10//3) #整除\n",
    "print(10%3) #取余数\n",
    "print(3*4)\n",
    "print(2**3)#2的3次方\n",
    "print(\"$\"*10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0541fdb-bc1c-438d-b1d5-5596a86a19e9",
   "metadata": {},
   "source": [
    "### 1.3.2、赋值运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "5ce8aa34-8b9f-41a3-9a4c-5177c22353bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 10 10\n",
      "12 33 44\n",
      "33 12\n",
      "2349470870096 10\n",
      "2349470870416 20\n",
      "2349470870096 10\n"
     ]
    }
   ],
   "source": [
    "a = b = c = 10\n",
    "print(a,b,c) #a,b,c都等于10，指向同一块内存\n",
    "a,b,c = 12,33,44\n",
    "print(a,b,c)#a为12，b为33\n",
    "a,b = b,a #直接交换\n",
    "print(a,b)\n",
    "a = 10\n",
    "print(id(a),a)\n",
    "a+=10 # a = a+10\n",
    "print(id(a),a)\n",
    "b = 10\n",
    "print(id(b),b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a1d692c-673b-4140-ac18-b72bee5fb4e2",
   "metadata": {},
   "source": [
    "### 1.3.3、比较运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "6a7ae8eb-49aa-4904-8ee9-9a1c4605ba73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n",
      "True 1000 1000\n",
      "False 1000 1000\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = 10\n",
    "print(a==b)\n",
    "print(a is b)\n",
    "a+=0\n",
    "print(a is b)\n",
    "c = d = 1000\n",
    "print(c is d,c,d)\n",
    "c+=0\n",
    "print(c is d,c,d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "641c74d5-030f-4114-ad05-968430716c20",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = 30\n",
    "c = (a>=10 and b<30) #或运算是用or\n",
    "c = (a>=10 or b<30)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6769353b-ed19-4da5-900e-69d59f3fcaeb",
   "metadata": {},
   "source": [
    "## 1.4、对象的布尔值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3efdb1e4-0308-4c22-aeb0-5c119d811dd7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(bool(False))\n",
    "print(bool(0))\n",
    "print(bool(0.0))\n",
    "print(bool(''))\n",
    "print(bool(None))\n",
    "print(bool(\"\")) #空字符\n",
    "print(bool([])) #空列表\n",
    "print(bool(())) #空元组\n",
    "print(bool({})) #空字典\n",
    "print(bool(dict())) #空字典\n",
    "print(bool(set())) #空集合\n",
    "print(bool(-1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b44895fa-2b25-4153-bbaf-c43614143a21",
   "metadata": {},
   "source": [
    "所有的对象，只要是空的元素都是False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92f0287e-1c73-4202-bdfc-0657ef1791b5",
   "metadata": {},
   "source": [
    "## 1.5、语句结构\n",
    "### 1.5.1、顺序结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ca6ac41d-41be-48b7-81f1-aa5de29f34f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "print(a) #输出的结果是10，代码是从上到下顺序执行的\n",
    "a = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82f49ef9-d34c-438e-95af-792129f18d16",
   "metadata": {},
   "source": [
    "### 1.5.2、选择结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fb2e7cc5-7718-472e-890a-d5ded903b274",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a>10\n",
      "30\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "if a==10:\n",
    "    print(\"a>10\")\n",
    "    print(30)\n",
    "print(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "af573686-2e2d-4a95-8f9d-1241292be797",
   "metadata": {},
   "outputs": [],
   "source": [
    "if a==10:\n",
    "    pass #占位符，表示什么都不做"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9c19616c-441f-4805-885b-020eb04ae6e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入成绩: 65\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不多不少，刚刚好!\n"
     ]
    }
   ],
   "source": [
    "ach = int(input(\"请输入成绩:\"))\n",
    "if ach>=90:\n",
    "    print(\"A\")\n",
    "elif ach>=80:\n",
    "    print(\"B\")\n",
    "elif ach>=70:\n",
    "    print(\"C\")65\n",
    "elif ach>=60:\n",
    "    print(\"不多不少，刚刚好!\")\n",
    "else:\n",
    "    print(\"完蛋啦!交钱重修!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3248cb4f-b7bb-4712-916a-d4876a6500c6",
   "metadata": {},
   "source": [
    "- 练习1:设定一个银行的存储金额，输出一个要取钱的数量，如果够就取钱，不够就提示不够，最后输出还剩下多少钱"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fa83a7dd-f827-4680-8d8d-4abecb7aba77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入取钱的数量 980\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "高高兴兴取了:980元\n",
      "银行余额:20\n"
     ]
    }
   ],
   "source": [
    "saved_money = 1000\n",
    "get_money = int(input(\"请输入取钱的数量\"))\n",
    "if get_money>saved_money:\n",
    "    print(\"呃！你的钱不够啊!\")\n",
    "else:\n",
    "    saved_money-=get_money\n",
    "    print(f\"高高兴兴取了:{get_money}元\")\n",
    "print(f\"银行余额:{saved_money}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76c7b7eb-2278-4fad-baba-68223e77c099",
   "metadata": {},
   "source": [
    "- 条件表达式\n",
    "条件表达式类似于c语言的三维运算符 a>b?a:b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "72588a7e-4429-4e03-a886-93badb162584",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20<30\n"
     ]
    }
   ],
   "source": [
    "a,b = 20,30\n",
    "print(f\"{a}>{b}\") if a>b else print(f\"{a}<{b}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f66ccc57-4755-432d-a24c-5f127f3d8ffd",
   "metadata": {},
   "source": [
    "### 1.5.3、循环结构\n",
    "- range结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "30b4b40c-07bc-4917-a3df-1ca1e1481350",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2203017050416 <class 'range'> 48\n",
      "2203017052048 <class 'range'> 48\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "----------------------------------------------------------------------------------------------------\n",
      "100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530,531,532,533,534,535,536,537,538,539,540,541,542,543,544,545,546,547,548,549,550,551,552,553,554,555,556,557,558,559,560,561,562,563,564,565,566,567,568,569,570,571,572,573,574,575,576,577,578,579,580,581,582,583,584,585,586,587,588,589,590,591,592,593,594,595,596,597,598,599,600,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671,672,673,674,675,676,677,678,679,680,681,682,683,684,685,686,687,688,689,690,691,692,693,694,695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,711,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,739,740,741,742,743,744,745,746,747,748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,765,766,767,768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799,800,801,802,803,804,805,806,807,808,809,810,811,812,813,814,815,816,817,818,819,820,821,822,823,824,825,826,827,828,829,830,831,832,833,834,835,836,837,838,839,840,841,842,843,844,845,846,847,848,849,850,851,852,853,854,855,856,857,858,859,860,861,862,863,864,865,866,867,868,869,870,871,872,873,874,875,876,877,878,879,880,881,882,883,884,885,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,930,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948,949,950,951,952,953,954,955,956,957,958,959,960,961,962,963,964,965,966,967,968,969,970,971,972,973,974,975,976,977,978,979,980,981,982,983,984,985,986,987,988,989,990,991,992,993,994,995,996,997,998,999,"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "r1 = range(10) ##会产生一个0-9的迭代器 range(10)相当于range(0,10)\n",
    "#所有的python的区间都遵从一个原则，叫做包头不包尾\n",
    "print(id(r1),type(r1),sys.getsizeof(r1))\n",
    "r2 = range(100,1000)\n",
    "print(id(r2),type(r2),sys.getsizeof(r2))\n",
    "# i从0开始一直到9\n",
    "for i in r1:\n",
    "    print(i)\n",
    "print(\"-\"*100)\n",
    "for i in r2:\n",
    "    print(i,end=\",\") #每一个输出不会换行，以逗号来连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ea7f62c8-f122-49d9-af49-676c7fd2b08e",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "老张开车去东北\n",
      "老张开车去东北\n",
      "老张开车去东北\n",
      "老张开车去东北\n",
      "老张开车去东北\n",
      "老张开车去东北\n",
      "老张开车去东北\n",
      "老张开车去东北\n",
      "老张开车去东北\n",
      "老张开车去东北\n"
     ]
    }
   ],
   "source": [
    "for _i in range(10):\n",
    "    print(\"老张开车去东北\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8dde8025-9a52-4f61-8c56-7084f19058e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "sum = 0\n",
    "while i<=100:\n",
    "    sum+=i # sum = sum+i\n",
    "    i+=1\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9cf31fe-864b-49ee-9504-425a18cfcd9f",
   "metadata": {},
   "source": [
    "- 输出80-900之间的偶数和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "90d7fc5e-ac53-461d-ab29-437e4fff1e6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "201390\n"
     ]
    }
   ],
   "source": [
    "sum = 0\n",
    "for i in range(80,901):\n",
    "    if i%2==0: #和2取余为0就表示偶数\n",
    "        sum+=i\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31b95dad-aedb-4a60-b201-b12490bf16e7",
   "metadata": {},
   "source": [
    "- 输出9x9乘法表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7a8f17b4-0c19-4078-a41d-56c833407817",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print(f\"{j}*{i}={j*i}\",end=\"\\t\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdd4ccdc-1483-4fbd-8f28-3a759f3d0eb9",
   "metadata": {},
   "source": [
    "- 输出圣诞树\n",
    "```shell\n",
    "*\n",
    "***\n",
    "*****\n",
    "*******\n",
    "*********\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "902ac5bb-033c-4b5e-b4cc-eacfcf5ce977",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\n",
      "***\n",
      "*****\n",
      "*******\n",
      "*********\n"
     ]
    }
   ],
   "source": [
    "layer = 5\n",
    "for i in range(1,layer+1):\n",
    "    #每一层的*数量\n",
    "    star_num = 2*i-1\n",
    "    for j in range(1,star_num+1):\n",
    "        print(\"*\",end=\"\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "1bc3e1bd-3dbd-4b15-b42f-02e83068d90c",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                             *\n",
      "                            ***\n",
      "                           *****\n",
      "                          *******\n",
      "                         *********\n",
      "                        ***********\n",
      "                       *************\n",
      "                      ***************\n",
      "                     *****************\n",
      "                    *******************\n",
      "                   *********************\n",
      "                  ***********************\n",
      "                 *************************\n",
      "                ***************************\n",
      "               *****************************\n",
      "              *******************************\n",
      "             *********************************\n",
      "            ***********************************\n",
      "           *************************************\n",
      "          ***************************************\n",
      "         *****************************************\n",
      "        *******************************************\n",
      "       *********************************************\n",
      "      ***********************************************\n",
      "     *************************************************\n",
      "    ***************************************************\n",
      "   *****************************************************\n",
      "  *******************************************************\n",
      " *********************************************************\n",
      "***********************************************************\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n",
      "                             *\n"
     ]
    }
   ],
   "source": [
    "layer = 30\n",
    "for i in range(1,layer+1):\n",
    "    #每一层的*数量\n",
    "    star_num = 2*i-1\n",
    "    #每一层的空格数\n",
    "    space_num = layer-i\n",
    "    print(\" \"*space_num,end=\"\")\n",
    "    for j in range(1,star_num+1):\n",
    "        print(\"*\",end=\"\")\n",
    "    print()\n",
    "for i in range(1,layer+1):\n",
    "     #每一层的空格数\n",
    "    space_num = layer-1\n",
    "    print(\" \"*space_num,end=\"\")\n",
    "    print(\"*\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91663823-8498-487a-84be-29fa6b7c9113",
   "metadata": {},
   "source": [
    "- break和continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b6d29eed-ff96-446b-90a4-f1e121c3b165",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    if i%5==0:\n",
    "        # continue\n",
    "        break\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66d5f4f7-b2ae-4388-8f28-8c2360d50db5",
   "metadata": {},
   "source": [
    "- python的循环结构可以使用else\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "81605854-9967-4b7d-b7b9-d6d23a16a34f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "end for\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    if i%5==0:\n",
    "        # continue;\n",
    "        break;\n",
    "    print(i)\n",
    "else:\n",
    "    print(\"end for\") #如果循环是正常结束的，就会执行else，如果中间break结束了，就不执行else"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d82c67a-276c-4d00-b961-c76298839a72",
   "metadata": {},
   "source": [
    "# 二、Python的集合类\n",
    "## 2.1、列表对象\n",
    "### 2.1.1、list基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "bed1892e-8ada-4cbc-b46f-8dc08e3b81c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12.3\n",
      "10,abc,12.3,False,\n",
      "2202958296128 <class 'list'>\n",
      "2202907667024 <class 'int'>\n",
      "2202909343600 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "lst = [10,\"abc\",12.3,False] #创建了一个列表对象\n",
    "print(lst[2])\n",
    "for i in lst: #遍历列表，类似c++中的迭代器\n",
    "    print(i,end=\",\")\n",
    "print()\n",
    "print(id(lst),type(lst))\n",
    "print(id(lst[0]),type(lst[0]))\n",
    "print(id(lst[1]),type(lst[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d971825e-ebe1-401d-a815-5e5e14cbe480",
   "metadata": {},
   "source": [
    "### 2.1.2、list创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "18cd316f-9df6-4e88-9d8e-9eae21615d37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19] 136\n",
      "[0, 4, 16, 36, 64]\n",
      "[1, 4, 3, 16, 5, 36, 7, 64, 9, 100, 11, 144, 13, 196, 15, 256, 17, 324, 19]\n"
     ]
    }
   ],
   "source": [
    "lst = list([1,2,3,4])\n",
    "print(lst)\n",
    "lst = list(range(10,20)) #创建之后马上分配内存\n",
    "import sys\n",
    "print(lst,sys.getsizeof(lst))\n",
    "#列表生成式\n",
    "lst2 = [x**2 for x in range(10) if x%2==0]\n",
    "print(lst2)\n",
    "lst2 = list(range(1,20))\n",
    "#偶数平方，奇数不变\n",
    "lst3 = [x if x%2!=0 else x**2 for x in lst2]\n",
    "print(lst3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55b4b53e-7074-4910-bfcd-0878eabecc74",
   "metadata": {},
   "source": [
    "### 2.1.3、列表的切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "2efad93b-9315-45b9-a5b6-c2f8ffa02bd4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
      "[10, 11, 12, 13]\n",
      "[10, 12, 14, 16, 18]\n",
      "[10, 13]\n",
      "[19, 18, 17, 16, 15, 14, 13, 12, 11, 10]\n",
      "[18, 16, 14, 12, 10]\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(10,20))\n",
    "print(lst)\n",
    "# lst[start:end:step] step默认为1\n",
    "print(lst[:4]) #lst[0:4:1]\n",
    "print(lst[::2]) #从第一个到最后一个，步长为2\n",
    "print(lst[0:5:3])\n",
    "print(lst[::-1])\n",
    "#倒序偶数下标\n",
    "print(lst[-2::-2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ca49d6e-6535-4f2f-8811-9295ede69be1",
   "metadata": {},
   "source": [
    "- 取出偶数下表的元素\n",
    "- 反转字符串\n",
    "- 隔两个取一位\n",
    "- 倒序偶数位\n",
    "- 列表中提取奇数位元素并且反向输出\n",
    "- 奇数和偶数交换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "24813666-ec5f-46f1-8ffa-ee3797c8e0d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[1, 3]\n"
     ]
    }
   ],
   "source": [
    "lst = [1,2,3,4]\n",
    "lst1 = lst[::2] #切片之后，list是不会发生变化的\n",
    "print(lst)\n",
    "print(lst1) #切片之后的新列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "eb8c8643-9f0b-4208-bf0f-cfabcbc478b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 2]\n"
     ]
    }
   ],
   "source": [
    "print(lst[1::2][::-1]) # lst[1::2]会得到一个lst之后进行lst[1::2][::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd6d0f12-f436-4c7c-b1e5-abf11fd4eaa5",
   "metadata": {},
   "source": [
    "### 2.1.4、列表的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69f0d205-ca2e-4e29-97f4-851092f9381f",
   "metadata": {},
   "source": [
    "- 列表的添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "c613c709-4261-4bd1-be34-1c06a69e7396",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9] 2203022637952\n",
      "2203022637952\n",
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 999]\n"
     ]
    }
   ],
   "source": [
    "lst = list(range(1,10))\n",
    "print(lst,id(lst))\n",
    "lst.append(11) #添加元素\n",
    "print(id(lst)) #lst是一个可变对象，添加元素和删除元素，都不会改变原来的值\n",
    "lst\n",
    "lst2 = lst\n",
    "lst2.append(999)\n",
    "print(lst)\n",
    "# lst.append(12)\n",
    "a = b = 10\n",
    "b = 20 #b指向了新内存，a没有改"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12015132-3389-4b62-8e81-229e68ad80ac",
   "metadata": {},
   "source": [
    "- 列表的删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "f2abda85-a0db-48a1-9027-2feb09d922ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['c', 'd', 'a']"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = [\"a\",\"b\",\"c\",\"d\",'a','x']\n",
    "lst.pop() #删除最后一个元素\n",
    "lst\n",
    "lst.pop(1) #删除位置为1的元素\n",
    "lst\n",
    "lst.remove('a')#删除一个具体值的元素\n",
    "lst\n",
    "lst.remove('a') #删除的元素不存在会抛出异常"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7113bfe-6c3e-4e50-8a6f-98a945e34684",
   "metadata": {},
   "source": [
    "- 列表的插入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "a1ceded1-dca3-4252-b7d9-51c63b41fd9d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, [22, 33, 44], 22, 3]\n",
      "[1, 22, 33, 44]\n",
      "[1, 22, 222, 333, 444, 33, 44]\n",
      "1\n",
      "33\n"
     ]
    }
   ],
   "source": [
    "lst = [1,2,3]\n",
    "lst.insert(2,22)\n",
    "lst\n",
    "lst.insert(2,[22,33,44])#输出结果是[1, 2, [22, 33, 44], 22, 3]\n",
    "print(lst)\n",
    "#基于切片的插入\n",
    "lst[1:] = [22,33,44]\n",
    "print(lst)\n",
    "lst[2:2] = [222,333,444]\n",
    "print(lst)\n",
    "print(lst.index(22))\n",
    "print(lst[-2])\n",
    "# print(lst.index(33232))#没有找到会抛出异常"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d38cf1b-113b-488c-a5d2-0d5384155651",
   "metadata": {},
   "source": [
    "- 列表的排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b08a69f3-d0ca-40ba-8ab7-a47f8730fbf7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[55, 23, 23, 23, 12, 11, 5]\n",
      "[23, 5, 12, 42, 1, 4] 2724776553280\n",
      "[1, 4, 5, 12, 23, 42] 2724781150144\n"
     ]
    }
   ],
   "source": [
    "lst = [23,55,23,11,12,5,23]\n",
    "# lst.sort()\n",
    "lst.sort(reverse=True) #是在本体进行排序，id不会发生变化\n",
    "print(lst)\n",
    "lst1 = [23,5,12,42,1,4]\n",
    "lst2 = sorted(lst1) #排序之后会产生一个新的列表\n",
    "print(lst1,id(lst1))\n",
    "print(lst2,id(lst2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "243e569c-2f82-4d49-b594-56f0af05307a",
   "metadata": {},
   "source": [
    "- 元素判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c5408cc0-dec5-454a-8f7c-789b600f2037",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "lst = [1,2,3,4,5]\n",
    "print(5 in lst)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d010aa38-ace5-41ed-9c71-14ad27088cc1",
   "metadata": {},
   "source": [
    "## 2.2、字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f57aa330-4a59-4b8e-a3ef-8c35170439e4",
   "metadata": {},
   "source": [
    "### 2.2.1、字典基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "92022b92-b3f2-4222-ba7e-806c9ed896b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 30, 'b': 20, 'c': 30, 'd': 50}"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {\"a\":10,\"b\":20,\"c\":30}\n",
    "d[\"a\"]\n",
    "d[\"d\"] = 50 #字典中添加一个元素d值为50\n",
    "d\n",
    "d[\"a\"] = 30 #此时会执行修改\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "762b3567-21bd-4b57-bb55-540051f3af6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "d = {\"a\":10,\"b\":20,\"c\":30}\n",
    "b = \"a\" in d #判断b是否是字典的键\n",
    "print(b)\n",
    "# print(d)\n",
    "# del d[\"a\"]\n",
    "d.pop('a') #删除键\n",
    "# d.pop()\n",
    "d\n",
    "b = \"a\" in d \n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "bb8cc040-b959-45dc-872b-07555f17c9cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张安'"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu = {\"no\":\"001\",\"name\":\"张安\",\"age\":22,\"major\":\"计算机科学技术\"}\n",
    "stu[\"name\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "c63207fe-47e1-4afe-a006-434fe8d5275a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名:   abc    \n"
     ]
    }
   ],
   "source": [
    "name = \"abc\"\n",
    "print(\"姓名:{:^10}\".format(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "e5d4d77a-d1f2-401a-a6ed-604a508111cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学号      姓名      年龄    专业             \n",
      "001       张三      22     计算机科学技术        \n"
     ]
    }
   ],
   "source": [
    "format_title = \"{:<8}{:<8}{:<6}{:<15}\"\n",
    "format_data = \"{:<10}{:<8}{:<7}{:<15}\"\n",
    "print(format_title.format(\"学号\",\"姓名\",\"年龄\",\"专业\"))\n",
    "print(format_data.format(\"001\",\"张三\",22,\"计算机科学技术\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9123d9f4-8856-4bff-9331-36b19c4b5b83",
   "metadata": {},
   "source": [
    "### 2.2.2、字典的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e13f8b1e-edac-4045-8461-96f65028debd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['a', 'b', 'c'])\n",
      "a:10\n",
      "b:20\n",
      "c:33\n",
      "dict_values([10, 20, 33])\n",
      "{}\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "# 创建字典的另外一种方式\n",
    "d = dict()#创建了一个空字典\n",
    "d = {\"a\":10,\"b\":20,\"c\":33}\n",
    "print(d.keys())\n",
    "#字典的遍历\n",
    "for key in d.keys():\n",
    "    print(f\"{key}:{d[key]}\")\n",
    "print(d.values())#获取所有的值\n",
    "d.clear() #字典的清理\n",
    "print(d)\n",
    "d = {\"a\":10,\"b\":20,\"c\":30}\n",
    "# v = d[\"aa\"] #找一个没有的会抛出异常\n",
    "v = d.get(\"aa\") #获取字典的元素，如果没有，会得到None\n",
    "v = d.get(\"a\")\n",
    "print(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "944b0311-43e4-4cee-8270-091e1ca13601",
   "metadata": {},
   "source": [
    "### 2.2.3、字典生成式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "cace696c-7f25-4609-aad1-4ab3d9095a1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 12, 'b': 22, 'c': 33}\n",
      "dict_items([('a', 12), ('b', 22), ('c', 33)])\n",
      "a 12\n",
      "b 22\n",
      "c 33\n"
     ]
    }
   ],
   "source": [
    "lst1 = [\"a\",\"b\",\"c\"]\n",
    "lst2 = [12,22,33]\n",
    "d = {k:v for k,v in zip(lst1,lst2)} #字典使用的是:\n",
    "print(d)\n",
    "dis = d.items()\n",
    "print(dis)\n",
    "for di in dis:\n",
    "    print(di[0],di[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5010f844-94c0-4b36-b90d-6ba803688db9",
   "metadata": {},
   "source": [
    "## 2.3、学生管理系统\n",
    "- 学生对象信息{\"no\":\"001\",\"name\":\"张安\",\"age\":22,\"major\":\"计算机科学技术\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "8064d281-c3f6-4a91-88f1-a2ab64570e01",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "欢迎使用学生管理系统!\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入操作 a\n",
      "请输入学号 001\n",
      "请输入姓名 zhangsan\n",
      "请输入年龄 12\n",
      "请输入专业 计算机\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===============>添加学生成功<=================\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入操作 a\n",
      "请输入学号 002\n",
      "请输入姓名 李四\n",
      "请输入年龄 32\n",
      "请输入专业 物联网\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===============>添加学生成功<=================\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入操作 l\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------\n",
      "学号      姓名      年龄    专业             \n",
      "001       zhangsan12     计算机            \n",
      "002       李四      32     物联网            \n",
      "------------------------------------------------------------\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入操作 q\n",
      "输入查询的学号: 002\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------\n",
      "学号      姓名      年龄    专业             \n",
      "002       李四      32     物联网            \n",
      "------------------------------------------------------------\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入操作 u\n",
      "输入要更新的学号 001\n",
      "请输入更新的姓名(zhangsan): 张三\n",
      "请输入年龄(12): 22\n",
      "请输入专业(计算机) \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入操作 l\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------\n",
      "学号      姓名      年龄    专业             \n",
      "001       张三      22     计算机            \n",
      "002       李四      32     物联网            \n",
      "------------------------------------------------------------\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入操作 d\n",
      "输入要删除的学号 002\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===============>当当当!删除学生成功<=================\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入操作 l\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------\n",
      "学号      姓名      年龄    专业             \n",
      "001       张三      22     计算机            \n",
      "------------------------------------------------------------\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入操作 e\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "感谢你使用我们的系统，下次再来!\n"
     ]
    }
   ],
   "source": [
    "format_title = \"{:<8}{:<8}{:<6}{:<15}\"\n",
    "format_data = \"{:<10}{:<8}{:<7}{:<15}\"\n",
    "stus = []\n",
    "def main():\n",
    "    #函数的代码\n",
    "    # print(\"run\")\n",
    "    print(\"欢迎使用学生管理系统!\")\n",
    "    while True:\n",
    "        print(\"请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\")\n",
    "        op = input(\"请输入操作\")\n",
    "        op = op.upper() #把小写转换为大写\n",
    "        if op=='A':\n",
    "            no = input(\"请输入学号\")\n",
    "            name = input(\"请输入姓名\")\n",
    "            age = int(input(\"请输入年龄\"))\n",
    "            major = input(\"请输入专业\")\n",
    "            sd = {\"no\":no,\"name\":name,\"age\":age,\"major\":major}\n",
    "            stus.append(sd)\n",
    "            print(\"===============>添加学生成功<=================\")\n",
    "        elif op=='D':\n",
    "            no = input(\"输入要删除的学号\")\n",
    "            stu = None\n",
    "            for s in stus:\n",
    "                if s[\"no\"]==no:\n",
    "                    stu = s\n",
    "            if stu:\n",
    "                stus.remove(stu)\n",
    "                print(\"===============>当当当!删除学生成功<=================\")\n",
    "            else:\n",
    "                print(\"********************删除的学生不存在!*******************************\")\n",
    "        elif op=='U':\n",
    "            ##1、输入要更新的学号\n",
    "            no = input(\"输入要更新的学号\")\n",
    "            ##2、根据学号找到学生对象\n",
    "            stu = None\n",
    "            for s in stus:\n",
    "                if s[\"no\"]==no:\n",
    "                    stu = s\n",
    "            if stu:\n",
    "                ##执行更新\n",
    "                name = input(f\"请输入更新的姓名({stu['name']}):\")\n",
    "                age = int(input(f\"请输入年龄({stu['age']}):\"))\n",
    "                major = input(f\"请输入专业({stu['major']})\")\n",
    "                if name:\n",
    "                    stu[\"name\"] = name\n",
    "                if age:\n",
    "                    stu[\"age\"] = age\n",
    "                if major:\n",
    "                    stu[\"major\"] = major\n",
    "            else:\n",
    "                print(\"********************更新的学生不存在!*******************************\")\n",
    "        elif op=='Q':\n",
    "            no = input(\"输入查询的学号:\")\n",
    "            #查询学生对象\n",
    "            stu = None\n",
    "            for s in stus:\n",
    "                if s[\"no\"]==no:\n",
    "                    stu = s\n",
    "            if stu:\n",
    "                #说明学生存在!\n",
    "                print(\"-\"*60)\n",
    "                #显示标题\n",
    "                print(format_title.format(\"学号\",\"姓名\",\"年龄\",\"专业\"))\n",
    "                print(format_data.format(stu[\"no\"],stu[\"name\"],stu[\"age\"],stu[\"major\"]))\n",
    "                print(\"-\"*60)\n",
    "            else:\n",
    "                print(\"********************查询的学生不存在!*******************************\")\n",
    "        elif op=='L':\n",
    "            print(\"-\"*60)\n",
    "            #显示标题\n",
    "            print(format_title.format(\"学号\",\"姓名\",\"年龄\",\"专业\"))\n",
    "            for stu in stus:\n",
    "                #每一个stu就是一个字典\n",
    "                print(format_data.format(stu[\"no\"],stu[\"name\"],stu[\"age\"],stu[\"major\"]))\n",
    "            print(\"-\"*60)\n",
    "        elif op=='E':\n",
    "            print(\"感谢你使用我们的系统，下次再来!\")\n",
    "            break;\n",
    "        else:\n",
    "            print(\"********************请选择一个正确的操作!*******************************\")\n",
    "main()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2102b51-f8b8-4efb-97bc-8cb81f11962a",
   "metadata": {},
   "source": [
    "## 2.4、元组操作\n",
    "元组类似于列表，区别是元组是不可变类型，使用()来创建，方法少，效率高"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a21f0dca-aabd-47d7-a267-ffc6d68df762",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2724770881728 <class 'tuple'>\n",
      "[1, 2, 3, 4]\n",
      "[1111, 2, 3, 4]\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "t = (1,2,3)\n",
    "print(id(t),type(t))\n",
    "lst = [1,2,3]\n",
    "lst.append(4)\n",
    "print(lst)\n",
    "# t.append(1) // tuple是不可变序列\n",
    "lst[0] = 1111\n",
    "print(lst)\n",
    "# t[0] = 1111 #元组不可以修改\n",
    "t2 = (19,) #只有一个元素的元组后面加,\n",
    "print(type(t2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "a9990e22-5ed1-4e6d-9a97-4fe6ed97406d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<zip object at 0x0000027A69D62D40>\n",
      "a 10 ha\n",
      "b 20 wu\n",
      "c 30 hu\n",
      "d 40 ye\n"
     ]
    }
   ],
   "source": [
    "lst1 = ['a','b','c','d']\n",
    "lst2 = [10,20,30,40]\n",
    "lst3 = ['ha','wu','hu','ye']\n",
    "tt = zip(lst1,lst2,lst3) #通过zip将所有的列表组合成一个zip对象\n",
    "print(tt)\n",
    "#zip里面是一个迭代器,zip里面放的是元组\n",
    "# for t in tt:\n",
    "#     print(t)\n",
    "# 如果执行上面的代码，等于迭代器到最后了，再遍历就没有了\n",
    "for a,b,c in tt:\n",
    "    print(a,b,c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f6561a8-1134-4e06-b912-e6862608765d",
   "metadata": {},
   "source": [
    "## 2.5、集合\n",
    "集合就是set使用{}来创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "d311a147-e137-4304-8380-db9f8f48ffde",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{3, 2, 123, 12} 2724785268320 <class 'set'>\n",
      "{2, 3, 12, 122, 123}\n",
      "{2, 3, 12, 123}\n",
      "{3, 12, 123}\n"
     ]
    }
   ],
   "source": [
    "s = {12,3,123,2,3}\n",
    "print(s,id(s),type(s))\n",
    "s.add(122)\n",
    "print(s)\n",
    "s.remove(122)\n",
    "print(s)\n",
    "# s.remove(111) #删除不存在的会报错\n",
    "s.discard(111) #删除不存在的不会报错\n",
    "s.discard(2)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "d4b42226-eed3-4b5d-b53f-57219a6fd250",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True 2724790417440 2724792749408\n"
     ]
    }
   ],
   "source": [
    "s1 = {10,20,30,40}\n",
    "s2 = {40,20,10,30}\n",
    "print(s1==s2,id(s1),id(s2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "68e8181e-f6b0-48c7-b73c-a21b0b659c77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "s1 = {10,20,30}\n",
    "s2 = {10,30,20,50}\n",
    "print(s1.issubset(s2))\n",
    "print(s2.issuperset(s1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "f1bc0b40-7a43-4bbe-820a-77728ab93995",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 10}\n",
      "{1, 2, 10}\n",
      "{1, 2, 10, 15, 50, 22, 30}\n",
      "{1, 2, 10, 15, 50, 22, 30}\n",
      "{50, 30}\n",
      "{50, 30}\n",
      "{50, 22, 30, 15}\n"
     ]
    }
   ],
   "source": [
    "s1 = {1,2,10,30,50}\n",
    "s2 = {22,10,15,1,2}\n",
    "print(s1.intersection(s2))#求交集\n",
    "print(s1 & s2)#求交集\n",
    "print(s1 | s2) #求并集\n",
    "print(s1.union(s2)) #求并集\n",
    "print(s1.difference(s2))\n",
    "print(s1-s2) #求差集\n",
    "#求对称差\n",
    "print(s1.symmetric_difference(s2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "285041f5-619b-41b5-816e-451b2f70eb86",
   "metadata": {},
   "source": [
    "# 三、函数\n",
    "## 3.1、函数基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "c6635e98-e27a-4af3-9945-23aef1cbe34d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "34\n",
      "[1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "def add(a,b):\n",
    "    return a+b\n",
    "c = add(12,22)\n",
    "print(c)\n",
    "a = b = 10\n",
    "a = 20\n",
    "a = [1,2,3]\n",
    "b = a\n",
    "b.append(4)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "ba7954bc-a72a-4d8c-a110-c1caebd31e6a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 20\n",
      "[2, 1, 3]\n"
     ]
    }
   ],
   "source": [
    "def swap(a,b):\n",
    "    z = a\n",
    "    a = b\n",
    "    b = z\n",
    "\n",
    "a = 10\n",
    "b = 20\n",
    "swap(a,b)\n",
    "print(a,b)\n",
    "\n",
    "def swap(a):\n",
    "    a[0],a[1] = a[1],a[0]\n",
    "#可变类型会交换\n",
    "lst = [1,2,3]\n",
    "swap(lst)\n",
    "print(lst)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8f3f7f5-3209-4fc7-9b47-f428d5b066c1",
   "metadata": {},
   "source": [
    "## 3.2、函数的其他写法\n",
    "### 3.2.1、函数的默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "1365b63b-9589-446c-9fb5-3e4d7e6c1142",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "134\n",
      "67\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def fn1(a,b,c=100):\n",
    "    return a+b+c\n",
    "# r = fn1(11) #传递参数，非默认的必须传递\n",
    "r = fn1(12,22)\n",
    "print(r)\n",
    "r1 = fn1(12,22,33)\n",
    "print(r1)\n",
    "#默认参数只能放在最后，否则报错\n",
    "# def fn2(a=10,b,c):\n",
    "#     pass\n",
    "# fn2(10,20)\n",
    "\n",
    "def fn2(a=90,b=10,c=20):\n",
    "    return a+b+c\n",
    "fn2(12)#12赋值给a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a12bb75a-1180-406c-b6eb-67f2cc8e6c61",
   "metadata": {},
   "source": [
    "### 3.2.2、列表和字典参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "93764052-086d-4124-af83-e454c7146053",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2724792388000 <class 'tuple'> (1, 2, 3, 4)\n",
      "10\n",
      "2724784240960 <class 'tuple'> (10, 20, 30)\n",
      "60\n"
     ]
    }
   ],
   "source": [
    "# *arg表示参数类型是列表\n",
    "def fn1(*arg):\n",
    "    print(id(arg),type(arg),arg)\n",
    "    #arg就是一个列表\n",
    "    sum = 0\n",
    "    for i in arg:\n",
    "        sum+=i\n",
    "    return sum\n",
    "\n",
    "r = fn1(1,2,3,4)\n",
    "print(r)\n",
    "# r2 = fn2((1,2,3,4))\n",
    "# r2 = fn1([1,2,3])\n",
    "lst = [10,20,30]\n",
    "r2 = fn1(*lst)\n",
    "print(r2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "daa1e76e-d186-41f5-8183-995daaf804d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2724791910208 <class 'dict'> {'a': 10, 'b': 20, 'c': 30}\n",
      "2724791910208 <class 'dict'> {'name': '张三', 'age': 22}\n"
     ]
    }
   ],
   "source": [
    "def fn1(**arg):\n",
    "    print(id(arg),type(arg),arg)\n",
    "fn1(a=10,b=20,c=30)\n",
    "d = {\"name\":\"张三\",\"age\":22}\n",
    "fn1(**d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "26fb91d6-0039-4c58-b919-7467cb71adbc",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (2733554704.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[82], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    def fn2(**a1,*a2):\u001b[0m\n\u001b[1;37m                 ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "#只能先列表，后字典\n",
    "def fn2(*a1,**a2):\n",
    "    print(a1,a2)\n",
    "lst = [1,2,3]\n",
    "d = {\"a\":1,\"b\":2}\n",
    "fn2(*lst,**d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45e9baf1-3d5a-43a9-ac82-669bd3c3dea9",
   "metadata": {},
   "source": [
    "## 3.3、雇员管理第一版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0019179-ac51-4222-9e87-7dbc52e55a04",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "欢迎使用奇思雇员管理系统!\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "选择具体的操作: a\n",
      "请输入员工编号: 001\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入员工姓名 刘博文\n",
      "请输入薪水 19900\n",
      "请输入部门 蜀国\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "====================>当当当!添加员工成功!<********************\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "选择具体的操作: a\n",
      "请输入员工编号: 002\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'eno': '001', 'name': '刘博文', 'salary': 19900, 'dep': '蜀国'}]\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入员工姓名 刘备\n",
      "请输入薪水 23232\n",
      "请输入部门 魏国\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "====================>当当当!添加员工成功!<********************\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "选择具体的操作: q\n",
      "输入查询类型:1(根据员工号查询)2(根据姓名查询) 2\n",
      "输入要查询的姓名: 刘\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "编号        姓名        薪水      部门             \n",
      "001         刘博文       19900    蜀国             \n",
      "002         刘备        23232    魏国             \n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "选择具体的操作: a\n",
      "请输入员工编号: 003\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'eno': '001', 'name': '刘博文', 'salary': 19900, 'dep': '蜀国'}, {'eno': '002', 'name': '刘备', 'salary': 23232, 'dep': '魏国'}]\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入员工姓名 张备备\n",
      "请输入薪水 2323\n",
      "请输入部门 蜀国\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "====================>当当当!添加员工成功!<********************\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "选择具体的操作: q\n",
      "输入查询类型:1(根据员工号查询)2(根据姓名查询) 备\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********************查询条件有误********************\n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "选择具体的操作: q\n",
      "输入查询类型:1(根据员工号查询)2(根据姓名查询) 2\n",
      "输入要查询的姓名: 备\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "编号        姓名        薪水      部门             \n",
      "002         刘备        23232    魏国             \n",
      "003         张备备       2323     蜀国             \n",
      "请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\n"
     ]
    }
   ],
   "source": [
    "emps = []\n",
    "# emp = {\"eno\":\"001\",\"name\":\"张三\",\"salary\":1000,\"dep\":\"财务处\"}\n",
    "def show_operator():\n",
    "    print(\"请选择具体的操作:添加(A)|删除(D)|更新(U)|查询(Q)|列表(L)|退出(E)\")\n",
    "\n",
    "def show_title():\n",
    "    format_title = \"{:<10}{:<10}{:<8}{:<15}\"\n",
    "    print(format_title.format(\"编号\",\"姓名\",\"薪水\",\"部门\"))\n",
    "\n",
    "def show_data(emp):\n",
    "    format_data = \"{:<12}{:<10}{:<9}{:<15}\"\n",
    "    print(format_data.format(emp['eno'],emp['name'],emp['salary'],emp['dep']))\n",
    "\n",
    "def get_emp(no):\n",
    "    print(emps)\n",
    "    for emp in emps:\n",
    "        if(no==emp['eno']):\n",
    "            return emp #返回字典对象\n",
    "    return None\n",
    "\n",
    "def get_emps(name):\n",
    "    #根据姓名获取\n",
    "    temps = []\n",
    "    for emp in emps:\n",
    "        if emp['name'].find(name) !=-1:\n",
    "            temps.append(emp)\n",
    "    return temps\n",
    "    \n",
    "def add():\n",
    "    #1、输入员工编号\n",
    "    no = input(\"请输入员工编号:\")\n",
    "    emp = get_emp(no) #获取员工字典对象\n",
    "    if emp:\n",
    "        err_msg(\"添加的员工已经存在!不能添加\")\n",
    "        return\n",
    "    #2、输入其他信息\n",
    "    name = input(\"请输入员工姓名\")\n",
    "    salary = int(input(\"请输入薪水\"))\n",
    "    dep = input(\"请输入部门\")\n",
    "    emp = {}\n",
    "    emp[\"eno\"] = no\n",
    "    emp[\"name\"] = name\n",
    "    emp[\"salary\"] = salary\n",
    "    emp[\"dep\"] = dep\n",
    "    emps.append(emp)\n",
    "    suc_msg(\"当当当!添加员工成功!\")\n",
    "\n",
    "def delete():\n",
    "    no = input(\"请输入员工编号\")\n",
    "    emp = get_emp(no)\n",
    "    if emp==None:\n",
    "        err_msg(\"删除的员工不存在!\")\n",
    "        return\n",
    "    emps.remove(emp)\n",
    "    suc_msg(\"咣咣咣!删除员工成功!\")\n",
    "        \n",
    "\n",
    "def update():\n",
    "    no = input(\"请输入员工编号\")\n",
    "    emp = get_emp(no)\n",
    "    if emp==None:\n",
    "        err_msg(\"更新的员工不存在!\")\n",
    "        return\n",
    "    name = input(f\"请输入员工姓名({emp['name']})\")\n",
    "    salary = input(f\"请输入薪水({emp['salary']})\")\n",
    "    dep = input(f\"请输入部门({emp['dep']})\")\n",
    "    if name:\n",
    "        emp[\"name\"] = name\n",
    "    if salary:\n",
    "        emp[\"salary\"] = int(salary)\n",
    "    if dep:\n",
    "        emp[\"dep\"] = dep\n",
    "    suc_msg(\"呼呼呼!更新员工成功!\")\n",
    "\n",
    "def query():\n",
    "    op = input(\"输入查询类型:1(根据员工号查询)2(根据姓名查询)\")\n",
    "    if op==\"1\":\n",
    "        no = input(\"输入查询编号\")\n",
    "        emp = get_emp(no)\n",
    "        if emp:\n",
    "            show_title()\n",
    "            show_data(emp)\n",
    "        else:\n",
    "            err_msg(\"查询的员工不存在哟!\")\n",
    "    elif op==\"2\":\n",
    "        name = input(\"输入要查询的姓名:\")\n",
    "        temps = get_emps(name)\n",
    "        if len(temps)>0:\n",
    "            show_title()\n",
    "            for e in temps:\n",
    "                show_data(e)\n",
    "        else:\n",
    "            err_msg(\"查询的员工不存在哟!\")\n",
    "            \n",
    "    else:\n",
    "        err_msg(\"查询条件有误\")\n",
    "\n",
    "def find():\n",
    "    show_title()\n",
    "    for emp in emps:\n",
    "        show_data(emp)\n",
    "\n",
    "def err_msg(msg):\n",
    "    print(\"*\"*20,end=\"\")\n",
    "    print(msg,end=\"\")\n",
    "    print(\"*\"*20)\n",
    "\n",
    "def suc_msg(msg):\n",
    "    print(\"=\"*20,end=\">\")\n",
    "    print(msg,end=\"<\")\n",
    "    print(\"*\"*20)\n",
    "    \n",
    "def main():\n",
    "    print(\"欢迎使用奇思雇员管理系统!\")\n",
    "    while True:\n",
    "        show_operator()\n",
    "        op = input(\"选择具体的操作:\")\n",
    "        op = op.upper()\n",
    "        if op=='A':\n",
    "            add()\n",
    "        elif op=='D':\n",
    "            delete()\n",
    "        elif op=='U':\n",
    "            update()\n",
    "        elif op=='Q':\n",
    "            query()\n",
    "        elif op=='L':\n",
    "            find()\n",
    "        elif op=='E':\n",
    "            print(\"欢迎再次使用我们的系统!\")\n",
    "            break\n",
    "        else:\n",
    "            err_msg(\"请选择一个正确的操作\")\n",
    "\n",
    "main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "9b2973df-8b6c-4cb4-bb4c-5ccd97d2fdc0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "s1 = \"abc\"\n",
    "# s1.index('d')\n",
    "s1.find('c')\n",
    "lst = [1,2,3,4]\n",
    "print(len(lst))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "132512c4-b385-4755-8b52-8074f1b90b38",
   "metadata": {},
   "source": [
    "# 四、字符串操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "86cdd7fd-e7b2-4ee4-bac4-399af3240c3c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1001 1001 1001\n",
      "1906328156208 1906328156208 1906330828016\n",
      "10 10 1906232414256 1906330344432\n"
     ]
    }
   ],
   "source": [
    "s1 = \"1001\"\n",
    "s2 = '1001'\n",
    "s3 = '100'\n",
    "s3+='1'\n",
    "print(s1,s2,s3)\n",
    "print(id(s1),id(s2),id(s3))\n",
    "s1 = '10'\n",
    "s2 = '1'\n",
    "s2+='0'\n",
    "print(s1,s2,id(s1),id(s2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "641646d0-8172-4110-8508-df75c6145c3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1906223114832 1906223115152\n",
      "1906223114832 1906223115152\n",
      "10 20\n"
     ]
    }
   ],
   "source": [
    "#函数传值的内存分析\n",
    "def swap(a,b):\n",
    "    print(id(a),id(b))\n",
    "    a,b = b,a\n",
    "a = 10\n",
    "b = 20\n",
    "print(id(a),id(b))\n",
    "swap(a,b)\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a1172ac-2fd7-435e-99fd-98e6fe1426cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def swap(a):\n",
    "    a[0],a[1] = a[1],a[0]\n",
    "lst = [1,2]\n",
    "swap(lst)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38f1c7d7-051c-417f-bc7c-b6e4f3fa6b42",
   "metadata": {},
   "source": [
    "## 4.1、字符串的基础操作\n",
    "字符串相当于列表，可以直接使用列表的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "05c034a1-5ddf-44c9-a0b9-076f5ef4a70d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1906300035504\n",
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n",
      "1906331204208\n",
      "helloa\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "s1 = 'hello'\n",
    "len(s1) #取长度\n",
    "s1[1] #取出e字符\n",
    "print(id(s1))\n",
    "#遍历字符串\n",
    "for i in s1:\n",
    "    print(i)\n",
    "# s1[1] = 'y' #字符串和元组一样，是不能进行修改的\n",
    "s1+='a'\n",
    "print(id(s1))\n",
    "print(s1)\n",
    "# len(s1)\n",
    "print('a' in s1) #操作和列表类似"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c13c6129-18c4-4026-891d-41db9c7cfa05",
   "metadata": {},
   "source": [
    "## 4.2、字符串的常用操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5489329e-11b5-4818-b2d5-0b751830d824",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "9\n",
      "he.llo.txt txt\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "s = \"hello world\"\n",
    "# s.index('a') #没有找到子串会报错\n",
    "print(s.find('l')) #没有找到会返回-1\n",
    "print(s.rfind('l'))\n",
    "filename = \"he.llo.txt\"\n",
    "#取出文件的后缀名\n",
    "pos = filename.rfind('.')\n",
    "s1 = filename[pos+1:] #必须加:，这个表示切片,切片会产生新的字符串\n",
    "print(filename,s1)\n",
    "#判断字符串是否以txt结束\n",
    "print(filename.endswith('.txt'))\n",
    "#以xxx开头\n",
    "print(filename.startswith('hello'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "4ba9bbcc-e0e2-4a1b-8ab0-173e96883b96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello YES HELLO YES hello yes\n",
      "HELLO yes\n",
      "好好学习,天天向上\n"
     ]
    }
   ],
   "source": [
    "s1 = 'hello YES'\n",
    "s2 = s1.upper() #也会产生一个新的字符串\n",
    "s3 = s1.lower() #变小写\n",
    "s4 = s1.swapcase()\n",
    "print(s1,s2,s3)\n",
    "print(s4)\n",
    "s1 = \"good good study day day up\"\n",
    "s1 = '好好学习,天天向上'\n",
    "s2 = s1.title() #第一个字母变大写\n",
    "print(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "cc5aa58c-74bf-4718-b26a-0bead4db8f62",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'world', 'yes', 'ok']\n",
      "['1', '2', '3', '4']\n",
      "['1', '2', '3,4']\n",
      "['d:/test/aaa/bb', 'hello.txt']\n"
     ]
    }
   ],
   "source": [
    "s3 = \"hello world yes ok\"\n",
    "lst = s3.split()#默认情况以空格进行分割\n",
    "print(lst)#分割成列表\n",
    "s4 = '1,2,3,4'\n",
    "print(s4.split(sep=','))#以,进行分割\n",
    "print(s4.split(',',maxsplit=2))\n",
    "filepath = 'd:/test/aaa/bb/hello.txt' #文件路径\n",
    "#快速获取文件名和文件路径 d:/test/aaa/bb/    hello.txt\n",
    "print(filepath.rsplit(\"/\",maxsplit=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "b920ea1b-dabe-47ce-87fc-d2eb0d50bb19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,a,a,a,a,a hello,b,b,b,b,b\n"
     ]
    }
   ],
   "source": [
    "s4 = \"hello,a,a,a,a,a\"\n",
    "s5 = s4.replace('a','b') #把a替换成b\n",
    "print(s4,s5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7bd1891-2f65-4034-82b9-434ca5de678b",
   "metadata": {},
   "source": [
    "## 4.3、字符串的切片\n",
    "字符串的切片操作和列表完全一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "7b083eef-b549-4566-aced-3360b007573b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dlrow olleh\n",
      "l e\n",
      "rwolh\n"
     ]
    }
   ],
   "source": [
    "s = \"hello world\"\n",
    "print(s[::-1])\n",
    "print(s[1::2][::-1])\n",
    "print(s[-3::-2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0a5ff8b-f7a3-4052-af88-bf5e4771eeaa",
   "metadata": {},
   "source": [
    "## 4.4、字符串的判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "55feb452-83db-4e84-8828-8fafa4e10790",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. False\n",
      "2. True\n",
      "3. True\n",
      "True\n",
      "True\n",
      "False\n",
      "True\n",
      "True\n",
      "True\n",
      "a   a|\n"
     ]
    }
   ],
   "source": [
    "s = 'hello,python'\n",
    "#1. = 10\n",
    "print('1.',s.isidentifier()) #是否是合法标识符 False\n",
    "print('2.','hello'.isidentifier())\n",
    "print('3.','张三_'.isidentifier())\n",
    "\n",
    "print('     '.isspace()) #是否是所有空白\n",
    "print('abcAA'.isalpha()) #是否全部是字符\n",
    "print('abc.'.isalpha()) #是否全部是字符\n",
    "\n",
    "print('123'.isdecimal())#是否是十进制的数字组成\n",
    "\n",
    "print('一二壹'.isnumeric())#是否是数字，True\n",
    "print('abc123'.isalnum()) #是否是数字和字母\n",
    "s1= '   a   a   ';\n",
    "print(s1.strip()+\"|\") #去除前后的空格"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35a48b4e-9e24-4cfd-afce-c1b01557c6c6",
   "metadata": {},
   "source": [
    "##4.5、字符串的比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "5cb9ad9f-bde1-4bd4-9cca-bafe194b9730",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "97 98 20013\n",
      "a b\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print('apple'>'app')#字母序列比较\n",
    "print('apple'>'banana')#a的字母序列小于b的\n",
    "print(ord('a'),ord('b'),ord('中')) #获取字母的ASCII\n",
    "print(chr(97),chr(98))\n",
    "\n",
    "#由于字符串驻留的原因，多数字符的内存都是一块空间，因为str是不可变序列\n",
    "a = \"10\"\n",
    "b = \"10\"\n",
    "c = \"10\"\n",
    "print(a==b) #True\n",
    "print(b is c) #True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d2ab71f-d622-4e90-a9ed-e60fef0b6339",
   "metadata": {},
   "source": [
    "## 4.5、格式化字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86f3a35b-38cb-4ebb-b319-cf6b716301d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = '张三'\n",
    "age = 20\n",
    "print('我叫%s,今年%d'%(name,age))\n",
    "\n",
    "print('我叫{0},今年{1}'.format(name,age))\n",
    "print(f'我叫{name},今年{age}岁')\n",
    "\n",
    "print('%10d' %99)#10表示宽度\n",
    "print('%.3f'%3.1415926)\n",
    "print('%10.3f'%3.1415926) #宽度为10,小数为3位\n",
    "\n",
    "print('{0:.3f}'.format(3.1415926))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9e68450-860b-4646-b996-2f3d274ac103",
   "metadata": {},
   "source": [
    "# 五、面向对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "086cdd3c-81e6-443a-b507-fd3b5fea93c5",
   "metadata": {},
   "source": [
    "## 5.1、面向对象基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "6a6f05e5-f14d-400e-bca9-aa44201712af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "lst = [1,2,3]\n",
    "print(type(lst))\n",
    "lst = list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "5c6710e7-7170-4193-9f8e-d73072c53895",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1906344117344 <class '__main__.Dog'>\n",
      "旺财:呜呜呜呜!\n"
     ]
    }
   ],
   "source": [
    "class Dog:\n",
    "    # 构造函数，又两个参数，一个是name，一个age\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def bark(self):\n",
    "        if self.age<2:\n",
    "            print(f\"{self.name}:叽叽叽\")\n",
    "        elif self.age<13:\n",
    "            print(f\"{self.name}:旺旺旺\")\n",
    "        else:\n",
    "            print(f\"{self.name}:呜呜呜呜!\")\n",
    "            \n",
    "d = Dog(\"旺财\",16) #创建了一只狗\n",
    "print(id(d),type(d))\n",
    "d.bark()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "555924f6-34ba-4cf6-b1a5-e846d02b183b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1906292709824 <class 'type'>\n",
      "1906276547504 <class '__main__.Person'>\n",
      "张三:22\n",
      "李四:55\n",
      "16\n",
      "16\n",
      "['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'ach', 'age', 'name', 'talk']\n",
      "['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name', 'talk']\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def talk(self):\n",
    "        print(f\"{self.name}:{self.age}\")\n",
    "        \n",
    "print(id(Person),type(Person))\n",
    "p1 = Person(\"张三\",22)\n",
    "print(id(p1),type(p1))\n",
    "p2 = p1\n",
    "#类是一个可变类型\n",
    "# p1.talk()\n",
    "# p2.talk()\n",
    "# p2.age = 33\n",
    "# p1.talk()\n",
    "p3 = Person(\"李四\",33)\n",
    "p3.age = 55\n",
    "p1.talk()\n",
    "p3.talk()\n",
    "p1.ach = 16\n",
    "print(p1.ach)\n",
    "print(p2.ach)\n",
    "# print(p3.ach) #p3是没有ach的\n",
    "print(dir(p1))\n",
    "print(dir(p3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "789434b8-b806-4fe4-920e-d7a4249c7de9",
   "metadata": {},
   "source": [
    "## 5.2、类属性和类方法和静态方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "07f64c66-cdd6-4027-aac4-3464c859424b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test1\n",
      "小明在学习!\n",
      "小王在学习!\n",
      "物联网\n",
      "计算机\n",
      "物联网\n",
      "小明在学习!\n"
     ]
    }
   ],
   "source": [
    "class Student:\n",
    "    classroom = '计算机'\n",
    "    def __init__(self,name,no):\n",
    "        self.name = name\n",
    "        self.no = no\n",
    "    def study(self):\n",
    "        print(self.name+\"在学习!\")\n",
    "\n",
    "    @classmethod #声明为该方法是类方法\n",
    "    def showClassroom(cls):\n",
    "        print(cls.classroom)\n",
    "\n",
    "class Test:\n",
    "    #静态方法，用来归类\n",
    "    @staticmethod\n",
    "    def test1():\n",
    "        print(\"test1\")\n",
    "        \n",
    "    @staticmethod\n",
    "    def test2():\n",
    "        print(\"test2\")\n",
    "\n",
    "Test.test1()\n",
    "\n",
    "s1 = Student(\"小明\",\"001\")\n",
    "s2 = Student('小王','002')\n",
    "s1.study()\n",
    "s2.study()\n",
    "s1.classroom = \"物联网\" #会为S1这个对象新建一个成员属性\n",
    "print(s1.classroom) #会访问自己的成员属性\n",
    "print(s2.classroom) #错误的，不能使用这种方式访问类属性\n",
    "#classroom是属于类的，写代码的时候不能使用对象来访问，要使用类名来访问\n",
    "Student.classroom = \"物联网\"\n",
    "Student.showClassroom()\n",
    "Student.study(s1) #不会报错!相当于s1.study()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "51fac296-bba5-4407-bc29-4309093e490a",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a10\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "fb() missing 1 required positional argument: 'self'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[96], line 12\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[38;5;66;03m##动态绑定了一个方法给b\u001b[39;00m\n\u001b[0;32m     11\u001b[0m a\u001b[38;5;241m.\u001b[39mb \u001b[38;5;241m=\u001b[39m fb\n\u001b[1;32m---> 12\u001b[0m \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mb\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: fb() missing 1 required positional argument: 'self'"
     ]
    }
   ],
   "source": [
    "class A:\n",
    "    def __init__(self,t1):\n",
    "        self.t1 = t1\n",
    "    def a(self):\n",
    "        print(f\"a{self.t1}\")\n",
    "a = A(10)\n",
    "a.a()\n",
    "\n",
    "def fb(self):\n",
    "    print(f\"bbbb{self.t1}\")\n",
    "##动态绑定了一个方法给b\n",
    "# a.b = fb\n",
    "a.b()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "636320e6-c94a-4216-8248-cce1e910dbc0",
   "metadata": {},
   "source": [
    "## 5.3、对象的继承"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "55745ec3-c68f-4ed1-b3e5-8a9d4d42aa58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小明--12--100\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    def __init__(self,name,age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "\n",
    "    def talk(self):\n",
    "        print(f\"{self.name}:{self.age}\")\n",
    "#学生继承于Person，就拥有Person的所有属性和方法\n",
    "class Student(Person):\n",
    "    def __init__(self,name,age,ach):\n",
    "        super().__init__(name,age) #调用父类的构造函数\n",
    "        self.ach = ach\n",
    "    #子类和父类的方法一样，会覆盖父类方法\n",
    "    def talk(self):\n",
    "        print(f\"{self.name}--{self.age}--{self.ach}\")\n",
    "\n",
    "stu = Student(\"小明\",12,100)\n",
    "stu.talk()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "2f26aa47-221e-452e-aff0-96ec8720e943",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A-->a\n",
      "aa\n",
      "bb\n",
      "['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']\n",
      "--------------------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['__class__',\n",
       " '__delattr__',\n",
       " '__dict__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__le__',\n",
       " '__lt__',\n",
       " '__module__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " '__weakref__',\n",
       " 'a',\n",
       " 'aa',\n",
       " 'bb']"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class A:\n",
    "    def a(self):\n",
    "        print(\"A-->a\")\n",
    "    def aa(self):\n",
    "        print(\"aa\")\n",
    "class B:\n",
    "    def a(self):\n",
    "        print(\"B-->a\")\n",
    "    def bb(self):\n",
    "        print(\"bb\")\n",
    "        \n",
    "#C同时继承于A和B，此时先继承先获取\n",
    "class C(A,B):\n",
    "    pass\n",
    "c = C()\n",
    "c.a()\n",
    "c.aa()\n",
    "c.bb()\n",
    "#所有的类都继承于object\n",
    "print(dir(object))\n",
    "print(\"-\"*20)\n",
    "dir(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf51e3d8-24bb-498a-9d8c-4f3e8a078912",
   "metadata": {},
   "source": [
    "## 5.4、电脑组装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "5bd32171-ebbd-4f44-a41a-e3bf1fa9aeb0",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "华硕 is run\n",
      "Intel-->I7\n",
      "Nivdia H2 VideoCard\n",
      "memory:8G\n",
      "win10 start......\n",
      "--------------------\n",
      "华硕 is run\n",
      "AMD-->毒龙\n",
      "Nivdia H2 VideoCard\n",
      "memory:8G\n",
      "win10 start......\n",
      "--------------------\n",
      "华硕 is run\n",
      "Intel-->I7\n",
      "Nivdia H2 VideoCard\n",
      "memory:8G\n",
      "win10 start......\n"
     ]
    }
   ],
   "source": [
    "class CPU:\n",
    "    def __init__(self,name,_type):\n",
    "        self.name = name\n",
    "        self._type = _type\n",
    "    def run(self):\n",
    "        print(f\"{self.name}-->{self._type}\")\n",
    "        \n",
    "class VideoCard:\n",
    "    def __init__(self,name,size):\n",
    "        self.name = name\n",
    "        self.size = size\n",
    "    def run(self):\n",
    "        print(f\"{self.name} VideoCard\")\n",
    "        print(f\"memory:{self.size}\")\n",
    "\n",
    "class MainBoard:\n",
    "    def __init__(self,name,cpu,video_card):\n",
    "        self.name = name\n",
    "        self.cpu = cpu\n",
    "        self.video_card = video_card\n",
    "    def startup(self):\n",
    "        print(f\"{self.name} is run\")\n",
    "        self.cpu.run()\n",
    "        self.video_card.run()\n",
    "        print(f\"win10 start......\")\n",
    "        \n",
    "c1 = CPU(\"Intel\",\"I7\")\n",
    "vc = VideoCard(\"Nivdia H2\",\"8G\")\n",
    "mb = MainBoard(\"华硕\",c1,vc)\n",
    "mb.startup()\n",
    "print(\"-\"*20)\n",
    "c2 = CPU(\"AMD\",\"毒龙\")\n",
    "mb.cpu = c2\n",
    "mb.startup()\n",
    "mb2 = mb\n",
    "mb2.cpu = c1 #mb2修改了，mb也修改，因为指向了同一块内存\n",
    "print(\"-\"*20)\n",
    "mb.startup()\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b5a5b82-57c7-471d-94fc-bd92eb8c5274",
   "metadata": {},
   "source": [
    "## 5.5、深拷贝和浅拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fa32a8bc-9bd1-4908-adbc-73d66cb13af7",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dell is run\n",
      "AMD-->I7\n",
      "Nivdia H2 VideoCard\n",
      "memory:8G\n",
      "win10 start......\n",
      "********************\n",
      "华硕 is run\n",
      "Intel-->I7\n",
      "Nivdia H2 VideoCard\n",
      "memory:8G\n",
      "win10 start......\n",
      "2361635523648 2361636260160 2361635523648\n"
     ]
    }
   ],
   "source": [
    "class CPU:\n",
    "    def __init__(self,name,_type):\n",
    "        self.name = name\n",
    "        self._type = _type\n",
    "    def run(self):\n",
    "        print(f\"{self.name}-->{self._type}\")\n",
    "        \n",
    "class VideoCard:\n",
    "    def __init__(self,name,size):\n",
    "        self.name = name\n",
    "        self.size = size\n",
    "    def run(self):\n",
    "        print(f\"{self.name} VideoCard\")\n",
    "        print(f\"memory:{self.size}\")\n",
    "\n",
    "class MainBoard:\n",
    "    def __init__(self,name,cpu,video_card):\n",
    "        self.name = name\n",
    "        self.cpu = cpu\n",
    "        self.video_card = video_card\n",
    "    def startup(self):\n",
    "        print(f\"{self.name} is run\")\n",
    "        self.cpu.run()\n",
    "        self.video_card.run()\n",
    "        print(f\"win10 start......\")\n",
    "c1 = CPU(\"Intel\",\"I7\")\n",
    "vc1 = VideoCard(\"Nivdia H2\",\"8G\")\n",
    "mb1 = MainBoard(\"华硕\",c1,vc1)\n",
    "mb2 = mb1\n",
    "import copy\n",
    "# mb3 = copy.copy(mb1)#执行了浅拷贝\n",
    "mb3 = copy.deepcopy(mb1) #执行了深拷贝\n",
    "# mb3.startup()\n",
    "mb3.name = \"dell\"\n",
    "#修改mb3中的cpu的值,会直接影响mb1中的cpu的值,此时使用的是copy.copy执行了浅拷贝,mb3的cpu和mb1的cpu指向了同一块内存\n",
    "mb3.cpu.name = \"AMD\"  \n",
    "#修改mb3中的cpu的值,不会直接影响mb1中的cpu的值,此时使用的是copy.deepcopy执行了深拷贝,mb3的cpu和mb1的cpu指向了不同的内存\n",
    "mb3.startup()\n",
    "print(\"*\"*20)\n",
    "mb1.startup()\n",
    "print(id(mb1),id(mb3),id(mb2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d78bb9f5-138e-4f17-929e-56864d79d62f",
   "metadata": {},
   "source": [
    "## 5.6、初始化顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b4da39b1-bc69-4f06-9ca2-2455721a7257",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "new方法被调用，cls的id为2361590562544\n",
      "obj被创建:id为2361630308480\n",
      "init被调用id为:2361630308480\n",
      "Dog:2361590562544\n",
      "2361630308480 <class '__main__.Dog'>\n",
      "new方法被调用，cls的id为2361590562544\n",
      "obj被创建:id为2361632830128\n",
      "init被调用id为:2361632830128\n",
      "****************************************\n",
      "new方法被调用，cls的id为2361590562544\n",
      "obj被创建:id为2361632829504\n"
     ]
    }
   ],
   "source": [
    "#所有类都继承于object\n",
    "class Dog:\n",
    "    def __init__(self,name):\n",
    "        print(f\"init被调用id为:{id(self)}\")\n",
    "        self.name = name\n",
    "        \n",
    "    def __new__(cls,*args,**kwargs):\n",
    "        print(f\"new方法被调用，cls的id为{id(cls)}\")\n",
    "        #super()的父类是object\n",
    "        obj = super().__new__(cls)\n",
    "        print(f\"obj被创建:id为{id(obj)}\")\n",
    "        #返回对象\n",
    "        return obj\n",
    "\n",
    "d = Dog(\"旺财\")\n",
    "print(f\"Dog:{id(Dog)}\")\n",
    "print(id(d),type(d))\n",
    "d2 = Dog(\"小强\")\n",
    "d3 = d #没有调用__new__因为只是指针指向d的内存\n",
    "print(\"*\"*40)\n",
    "import copy\n",
    "d4 = copy.copy(d) #会调用new，但是不会调用init"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "43247428-3b0b-4f8d-a622-222770b7f5eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'__module__': '__main__', '__init__': <function Dog.__init__ at 0x00000225DC726700>, '__str__': <function Dog.__str__ at 0x00000225DC726E50>, '__eq__': <function Dog.__eq__ at 0x00000225DC7261F0>, '__len__': <function Dog.__len__ at 0x00000225DBFA6040>, '__add__': <function Dog.__add__ at 0x00000225DCDA5E50>, '__doc__': None, '__hash__': None}\n",
      "{'base': 'animal', 'name': '旺财', 'age': 13, 'ok': 'yeah'}\n",
      "['__add__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'base', 'name', 'ok']\n",
      "name:旺财,age:13 name:旺财,age:13\n",
      "True 2361637446512 2361630620496\n",
      "False\n",
      "5\n",
      "26\n"
     ]
    }
   ],
   "source": [
    "class Animal:\n",
    "    def __init__(self):\n",
    "        self.base = \"animal\"\n",
    "class Dog(Animal):\n",
    "    def __init__(self,name,age):\n",
    "        super().__init__() #调用父类的初始化方法\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "    def __str__(self):\n",
    "        return f\"name:{self.name},age:{self.age}\"\n",
    "    #覆盖__eq__可以用来修改比较条件\n",
    "    def __eq__(self,obj):\n",
    "        return self.name==obj.name and self.age==obj.age\n",
    "    #覆盖len可以获取这个对象的大小\n",
    "    def __len__(self):\n",
    "        return len(self.name)\n",
    "    #覆盖之后可以执行两个对象相加\n",
    "    def __add__(self,obj):\n",
    "        return self.age + obj.age\n",
    "dir(Dog)\n",
    "print(Dog.__dict__)\n",
    "d = Dog(\"旺财\",13)\n",
    "d.ok =\"yeah\"\n",
    "print(d.__dict__) #查询自己的属性和值\n",
    "print(dir(d))\n",
    "print(d,d.__str__())#当直接输入d的之后，会调用父类的__str__()方法来获字符串，改方法是在object中定义\n",
    "\n",
    "d1 = Dog(\"旺财\",13)\n",
    "d2 = Dog(\"旺财\",13)\n",
    "#对象的比较，默认比较的是id，Id不一样==也就不一样\n",
    "#如果希望通过===来比较特殊的属性，需要重写__eq__\n",
    "#d1==d2相当于 d1.__eq__(d2)\n",
    "print(d1==d2,id(d1),id(d2))\n",
    "lst = [Dog(\"a\",32),Dog(\"b\",13),Dog(\"c\",32)]\n",
    "d3 = Dog(\"aaaaa\",32)\n",
    "print(d3 in lst)\n",
    "print(len(d3))\n",
    "print(d1+d2) #覆盖了__add__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3632321c-2f3c-475e-b5e8-e52668b851bd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
